package com.rfid.emergencywarehouse.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.base.base.BaseAdapter;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScanBarcodeAdapter extends BaseAdapter<ScanBarcodeAdapter.MyHolder,BarcodeInfo> {

    int mSelect = -1;
    protected boolean selectable = true;
    public ScanBarcodeAdapter(List<BarcodeInfo> data) {
        super(data);
    }

    public void setSelectable(boolean selectable) {
        this.selectable = selectable;
    }

    @Override
    protected int getLayout() {
        return R.layout.item_scan_barcode;
    }

    @Override
    protected MyHolder getViewHolder(View view) {
        return new MyHolder(view);
    }

    @Override
    protected void bindView(MyHolder holder, int position, BarcodeInfo t) {
        BarcodeInfo barcodeInfo = mData.get(position);
        holder.barcode.setText("RFID编码：" + barcodeInfo.getBarcode());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!selectable) {
                    return;
                }
                mSelect = position;
                if (mListener != null) {
                    mListener.onClick(mData.get(position));
                }
                notifyDataSetChanged();
            }
        });
//        holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (mListener != null) {
//                }
//            }
//        });
        holder.select.setClickable(false);
        if (position == mSelect) {
            holder.select.setChecked(true);
        } else {
            holder.select.setChecked(false);
        }
        if (!selectable){
            holder.select.setVisibility(View.GONE);
        } else {
            holder.select.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        if (mData.size() == 0) {
            mSelect = -1;
        }
        return super.getItemCount();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @Nullable
        @BindView(R.id.barcode)
        TextView barcode;
        @Nullable
        @BindView(R.id.status)
        TextView status;
        @Nullable
        @BindView(R.id.select)
        CheckBox select;
        @Nullable
        @BindView(R.id.kw)
        TextView kw;
        @Nullable
        @BindView(R.id.name)
        TextView name;
        @Nullable
        @BindView(R.id.model)
        TextView model;
        @Nullable
        @BindView(R.id.materialCategoryName)
        TextView materialCategoryName;
        @Nullable
        @BindView(R.id.wg_status)
        TextView wg_status;
        @Nullable
        @BindView(R.id.edit_wg)
        TextView edit_wg;
        @Nullable
        @BindView(R.id.pd_result)
        TextView pd_resualt;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
